package com.cloud.tsp.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.annotation.TransUser;
import com.cloud.api.domain.alarm.Accident;
import com.cloud.api.domain.alarm.AccidentExcel;
import com.cloud.api.domain.alarm.QueryAccident;
import com.cloud.api.domain.tsp.VehicleSale;
import com.cloud.api.utils.ExcelUtils;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.mapper.AccidentMapper;
import com.cloud.tsp.service.IAccidentService;
import com.cloud.tsp.service.IVehicleSaleService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2023-12-22
 */
@Service("accidentService")
public class AccidentServiceImpl extends ServiceImpl<AccidentMapper, Accident> implements IAccidentService {

    @Resource
    private AccidentMapper accidentMapper;
    @Resource
    private IVehicleSaleService vehicleSaleService;
    @Resource
    private MessageSource messageSource;

    @Override
    @TransUser
    public TableDataInfo<Accident> selectAccidentList(QueryAccident para) {
        Page<Accident> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<Accident> data = accidentMapper.selectAccidentList(page, para);
        for (Accident accident : data.getRecords()){
            if (StringUtil.isNotBlank(accident.getCarNumber())){
                if ("oil".equals(accident.getVehicleType())){
                    accident.setCarNumber(StrUtil.hide(para.getCarNumber(), 3, 6));
                }else {
                    accident.setCarNumber(StrUtil.hide(para.getCarNumber(), 4, 7));
                }
            }
            accident.setVin(StrUtil.hide(accident.getVin(), 8, 11));
        }

        return TableDataInfo.build(data);
    }

    @Override
    public void InsertOrUpdate(Accident para) {
        LambdaQueryWrapper<VehicleSale> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleSale::getVin,para.getVin())
                .select(VehicleSale::getDeptId);
        VehicleSale vehicleSale = vehicleSaleService.getBaseMapper().selectOne(queryWrapper);
        if (vehicleSale!= null){
            para.setDeptId(vehicleSale.getDeptId());
        }
        this.saveOrUpdate(para);
    }

    @Override
    public void exportAccidentList(QueryAccident para, HttpServletResponse response) throws IOException{
        Locale locale = LocaleContextHolder.getLocale();
        String fileName = "table.accident";
        List<Accident> list = accidentMapper.selectAccidentListCondition(para);
        List<AccidentExcel> accidentExcels = new ArrayList<>();
        for(Accident accident : list){
            AccidentExcel accidentExcel = new AccidentExcel();
            BeanUtils.copyProperties(accident,accidentExcel);
            accidentExcel.setAddr(accident.getProvince()+accident.getCity()+accident.getDistrict());
            accidentExcel.setFireVehicleStatus(accident.getFireVehicleStatus().toString());
            accidentExcel.setCommonChargeMethods(String.valueOf(accident.getCommonChargeMethods()));
            accidentExcel.setLastChargeMethods(String.valueOf(accident.getLastChargeMethods()));
            accidentExcel.setVin(StrUtil.hide(accident.getVin(), 8, 11));
            if (StringUtil.isNotBlank(accident.getCarNumber())){
                if ("oil".equals(accident.getVehicleType())){
                    accidentExcel.setCarNumber(StrUtil.hide(para.getCarNumber(), 3, 6));
                }else {
                    accidentExcel.setCarNumber(StrUtil.hide(para.getCarNumber(), 4, 7));
                }
            }
            accidentExcels.add(accidentExcel);
        }
        fileName = messageSource.getMessage(fileName, null, fileName, locale);
        ExcelUtils.export(response,fileName,accidentExcels, AccidentExcel.class);
    }
}